<?php
/**
 * Created by PhpStorm.
 * Author: 王晓云
 * Date: 2017/12/21 0021
 * Time: 15:15
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\paginator;
use app\admin\model\Admin as User;

class Admin extends Comm
{
    public function index()
    {
    }

    //管理员列表
    public function manager()
    {
        $user = new User;
        $data = $user->order("id", "desc")->paginate(10);
        $this->assign("list", $data);
        return $this->fetch();
    }

    //管理员添加
    public function addManager()
    {
        if (request()->isPost()) {
            $adminM = new User;
            $user = input('post.user_name');
            $pwd = md5(input('post.password'));
            $email = input('post.email');
            if ($adminM->data(['user' => $user, 'pwd' => $pwd, 'email' => $email])->save()) {
                $this->success("管理员添加成功！", "Admin/manager");
            } else {
                $this->error("管理员添加失败！", "Admin/addManager");
            }
            return;
        }
        return $this->fetch();
    }

    //管理员修改
    public function editManager()
    {
        $id = input("get.id");
        $user = new User;
        $result = $user->where("id", $id)->find();
        if (request()->isPost()) {
            $adminM = new User;
            $user = input('post.user_name');
            $id = input("post.id");
            //dump($_POST);
            $res = $adminM->where("id", $id)->find()->toArray();
            // dump($res);

            if (input("post.password") == "") {
                $pwd = $res["pwd"];
            } else {
                $pwd = md5(input("post.password"));
            }
            $email = input('post.email');
            if ($adminM->save(['user' => $user, 'pwd' => $pwd, 'email' => $email], ["id" => $id])) {
                $this->success("管理员修改成功！", "Admin/manager");
            } else {
                $this->error("管理员修改失败！", "Admin/addManager");
            }
            return;
        }
        $this->assign("res", $result);
        return $this->fetch();
    }

    //管理员删除
    public function delManager()
    {
        $id = input("get.id");
        $user = new User;
        if ($result = $user->where("id", $id)->delete()) {
            $this->success("管理员删除成功！", "Admin/manager");
        } else {
            $this->error("管理员删除失败！");
        }
    }

}

?>